package com.inibb.app.service.admin.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inibb.app.service.admin.RegionService;



@Transactional
@Service
public class RegionServiceImpl implements RegionService{

	
	/*@Autowired
	private HibernateCodeTableDAO codeTableDao;

	public List<Region> getAllRegions() {
		return codeTableDao.getHibernateTemplate().loadAll(Region.class);
	}
	
	public Map<String, String> getAllRegionsAsMap() {
		Map<String, String> map = new TreeMap<String, String>();
		List<Region> ls = this.getAllRegions();
		map.put("", "--");
		for(Region r: ls){
			map.put(r.getRegCode(), r.getRegName());
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	public Map<String, Region> getRegionsByUniqueIds(
			String uniquePropertyName, final List<Integer> uniquePropertyValues) {
		Map<String, Region> map = new LinkedHashMap<String, Region>();
		List<Region> result =  (List<Region>)codeTableDao.getHibernateTemplate().executeFind(new HibernateCallback<List<Region>>(){

			@Override
			public List<Region> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Region.class);
				criteria.add(Restrictions.in("id", uniquePropertyValues));
				return criteria.list();
			}
			
		});
		
		for(Region prd:result){
			map.put(prd.getIdStr(), prd);
		}
		return map;	
	}
	
	public void updateRegion(Region r) {
		codeTableDao.getHibernateTemplate().merge(r);
	}
	
	public void saveRegion(Region r) throws ServiceVLDException {
		Set<ConstraintViolation<Region>> errs = validator.validate(r);
		if(errs.isEmpty()){
			codeTableDao.getHibernateTemplate().save(r);
		}else{
			ServiceVLDException exp = JpaValidatorHelper.convertToException(errs, r, "region");
			throw exp;
		}
	}

	public synchronized void deleteRegion(Integer id) {
		Region r = getRegion(id);
		codeTableDao.getHibernateTemplate().delete(r, LockMode.UPGRADE_NOWAIT);
	}

	@Override
	public Region getRegion(String id) {
		return this.getRegion(Integer.valueOf(id));
	}
	
	public Region getRegion(Integer id) {
		return this.codeTableDao.getHibernateTemplate().get(Region.class, id);
	}

	@Override
	public int countPageRegions(final PaginationCriteria paginationCriteria) {
		Integer total = codeTableDao.getHibernateTemplate().execute(new HibernateCallback<Integer>(){

			@Override
			public Integer doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Region.class);
				
				HibernateCriteriaUtils.getHibernateFilterCriteria(criteria, paginationCriteria.getPageFilter());
				criteria.setProjection(Projections.rowCount());
				
				return (Integer)criteria.uniqueResult();
			}
			
		});
		if(total==null)
			return 0;
		else			
			return total;
	}

	@Override
	public List<Region> searchPageRegions(final PaginationCriteria paginationCriteria) {
		List<Region> ls = codeTableDao.getHibernateTemplate().execute(new HibernateCallback<List<Region>>(){

			@Override
			public List<Region> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Region.class);
				
				HibernateCriteriaUtils.getHibernateFilterAndSorterCriteria(criteria, paginationCriteria);
				criteria.setFirstResult(paginationCriteria.getRowStart());
				criteria.setMaxResults(paginationCriteria.getRowEnd()-paginationCriteria.getRowStart());
				
				return (List<Region>)criteria.list();
			}
			
		});
		return ls;
	}*/
}
